<?php

(defined('BASEPATH')) OR exit('No direct script access allowed');

class Ticket extends MX_Controller {

    function __construct() {
        parent::__construct();
        $this->load->model('ticket/ticket_model');
        $this->load->model('ticket/ticket_cat_model');
        $this->load->model('ticket/tag_model');
        $this->load->model('image/image_model');
        if(empty($this->category)) $this->load->library('category');
        $this->category->build_option_data(1, 'Categories', 0);
    }

    
    function list_ticket($idcat = 1) {
        $this->category->option_store;
        $this->category->build_children_array($idcat);
        $all_cates = $this->category->children_array;
        $all_cates[] = $idcat;
        
        $view_data = array();
        $options = $this->_get_search_form_data();
        if(!empty($all_cates)) $options['cat_id'] = $all_cates;
        $this->load->library('pagination');
        
        $config['base_url'] = site_url('ticket/list-ticket');
        $config['total_rows'] = $this->ticket_model->get_ticket_count($options);
        $config = array_merge($config, get_pagination_config());
        
        $this->pagination->initialize($config);
        
        $options['limit'] = $config['per_page'];
        $options['offset'] = $this->uri->segment(3);
        $options['ticket.language'] = 'fr';
        
        if (!isset($options['offset']) || !is_numeric($options['offset']) || (int) $options['offset'] < 0 || $options['offset'] > $config['total_rows'])
            $options['offset'] = 0;

        $view_data = $options;
        $view_data['tickets'] = $this->ticket_model->get_ticket($options);
        $view_data['category'] = $this->ticket_cat_model->get_categories_array();
        $view_data['image'] = $this->image_model->get_image_array();
        
        $view_data['pagination'] = $this->pagination->create_links();
//        return $view_data;
//        $this->load->view('ticket/list', $view_data);
        return $this->load->view('frontend/content', $view_data); 
    }
    
    private function _get_search_form_data() {

        $options = array();
        $options['quantity >'] = 0;
        $options['expiry_date >='] = date('Y-m-d H:i:s');
        return $options;
    }
    
    
    /**
     * @author duongbq
     * @return type 
     * Load for sidebar
     */
    function list_ticket_categories($idcat) {

        $view_data = array();
        $view_data['categories'] = $this->category->option_store;//$this->ticket_cat_model->get_categories();
        
        return $this->load->view('ticket/categories', $view_data, TRUE); // Return view for frontend controller
    }

    function list_ticket_tags() {
        
        $view_data = array();
        $view_data['tags'] = $this->ticket_model->get_all_tags();
        return $this->load->view('ticket/tags', $view_data, TRUE); 
    }
    
    function search($keyword = ''){
        if(empty($keyword) && !$this->input->post('searchkey')) redirect('/');
        if(empty($keyword)) $keyword = $this->input->post('searchkey');
        $view_data = array();
        $options = $this->_get_search_form_data();
        $this->load->library('pagination');
        
        $config['base_url'] = site_url('ticket/search/'.$keyword);
        $config['total_rows'] = count($this->ticket_model->search_ticket($keyword));
        $config = array_merge($config, get_pagination_config());
        
        $this->pagination->initialize($config);
        
        $options['limit'] = $config['per_page'];
        $options['offset'] = $this->uri->segment(3);
        
        if (!isset($options['offset']) || !is_numeric($options['offset']) || (int) $options['offset'] < 0 || $options['offset'] > $config['total_rows'])
            $options['offset'] = 0;

        $view_data = $options;
        $view_data['tickets'] = $this->ticket_model->search_ticket($keyword,$options['limit'],$options['offset']);
        $view_data['category'] = $this->ticket_cat_model->get_categories_array();
        $view_data['image'] = $this->image_model->get_image_array();
        $view_data['keyword'] = $keyword;
        $view_data['pagination'] = $this->pagination->create_links();
//        return $view_data;
//        $this->load->view('ticket/list', $view_data);
        return $this->load->view('frontend/search', $view_data);
    }
    
    function detail($id=0){
        if(empty($id) && !$this->uri->segment(3)) redirect('/');
        if(empty($id)) $id = $this->uri->segment(3);
        if(empty($id) || !is_numeric($id)) redirect('/');
        $get_one_ticket = $this->ticket_model->get_ticket(array('id'=>$id));
        if(empty($get_one_ticket)) redirect('/');
        $get_one_ticket = $get_one_ticket[0];
        $view_data = array();
        $view_data['one_ticket'] = $get_one_ticket;
        //$view_data['category'] = $this->ticket_cat_model->get_categories_array();
        $view_data['image'] = $this->image_model->get_image_array();
        
        $view_data['title'] = $get_one_ticket->meta_title;
        $view_data['keywords'] = $get_one_ticket->meta_keyword;
        $view_data['description']= $get_one_ticket->meta_description;
        
        return $this->load->view('frontend/details', $view_data);
    }
}
